package com.dqyt.app.mongo;

import com.dqyt.app.bean.Maintenance;
import com.dqyt.app.bean.Sample;
import com.mongodb.client.result.DeleteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 维修记录
 */
@Component
public class MaintenanceDao {
    /**
     * 注入MongoTemplate
     */
    @Autowired
    protected MongoTemplate mongoTemplate;


    /**
     * 新建数据到数据库
     *
     * @return
     */
    public void save(Maintenance maintenance) {
        mongoTemplate.save(maintenance); // 保存数据到数据库
    }




    /**
     * 分页查询维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public List<Maintenance> getMainteListPage(int pageNum, int pageSize, String bsbh, String erweima, int type, int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }


        if(wtfIdChi!=0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }

        if(null != rwdbh  && !rwdbh.equals("")){
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }



        query.limit(pageSize).skip(pageNum);
        query.with(Sort.by(Sort.Order.desc("addTime")));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list;
    }




    /**
     * 查询总条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public long getMainteListTotle(String bsbh,String erweima,int type,int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }


        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfIdChi!=0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }
        if(null != rwdbh  && !rwdbh.equals("")){
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list.size();
    }



    /**
     * 分页查询维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public List<Maintenance> getMainteListPageMe(int pageNum, int pageSize, String bsbh, String erweima, int type,
                                                 int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh,long wxyId){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }



        if(wtfIdChi!=0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }

        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }

        if(null != rwdbh  && !rwdbh.equals("")){
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        query.addCriteria(new Criteria("wxyId").is(wxyId));


        query.limit(pageSize).skip(pageNum);
        query.with(Sort.by(Sort.Order.desc("addTime")));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list;
    }


    /**
     * 查询总条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public long getMainteListTotleMe(String bsbh,String erweima,int type,int wtfId,int wtfIdChi,String jyrqNian,String jyrqYue,String rwdbh,long wxyId){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }


        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(wtfIdChi!=0){
            query.addCriteria(new Criteria("wtfIdChi").is(wtfIdChi));
        }

        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }
        if(null != rwdbh  && !rwdbh.equals("")){
            query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        }

        query.addCriteria(new Criteria("wxyId").is(wxyId));

        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list.size();
    }


    /**
     * 分页查询维修列表
     * @param pageNum  页数
     * @param pageSize  每页条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public List<Maintenance> getMyMainteListPage(int pageNum, int pageSize, String bsbh, String erweima, int type, int wtfId,String jyrqNian,String jyrqYue,long hyyId){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }
        query.addCriteria(new Criteria("hyzt").is(0));
        query.addCriteria(new Criteria("hyyId").is(hyyId));

        query.limit(pageSize).skip(pageNum);
        query.with(Sort.by(Sort.Order.desc("addTime")));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list;
    }

    /**
     * 查询总条数
     * @param bsbh   标识编号
     * @param erweima  二维码
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @param wtfId  委托方id
     * @return
     */
    public long getMyMainteListTotle(String bsbh,String erweima,int type,int wtfId,String jyrqNian,String jyrqYue,long hyyId){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        if (null != erweima && !erweima.equals("")) {
            query.addCriteria(new Criteria("erweima").is(erweima));
        }

        if(type != 0){
            query.addCriteria(new Criteria("type").is(type));
        }

        if(wtfId>0){
            query.addCriteria(new Criteria("wtfId").is(wtfId));
        }
        if(null != jyrqNian  && !jyrqNian.equals("")){
            query.addCriteria(new Criteria("mjyrqNian").is(jyrqNian));
        }

        if(null != jyrqYue  && !jyrqYue.equals("")){
            query.addCriteria(new Criteria("mjyrqYue").is(jyrqYue));
        }

        query.addCriteria(new Criteria("hyyId").is(jyrqYue));
        query.with(Sort.by(Sort.Order.asc("addTime")));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list.size();
    }


    /**
     * 查询总条数
     * @param
     * @param
     * @return
     */
    public long getMyMainteListTotle2(long hyyId){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("hyyId").is(hyyId));
        query.addCriteria(new Criteria("hyzt").is(0));
        query.with(Sort.by(Sort.Order.asc("addTime")));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list.size();
    }

    /**
     * 查询这批单号中维修了多少个   必须是已经审核签字了的
     * @return
     */
    public List<Maintenance>  getMaintenanceByRwdbh(String rwdbh){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(new Criteria("rwdbh").is(rwdbh));
        query.addCriteria(new Criteria("hyzt").is(1));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list;

    }

    /**
     * 查询维修记录
     * @param bsbh
     * @return
     */
    public Maintenance getMainteBybsbh(String bsbh){
        // 构建查询条件
        Query query = new Query();
        if (null != bsbh && !bsbh.equals("")) {
            query.addCriteria(new Criteria("bsbh").is(bsbh));
        }
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        if(list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }




    /**
     * 查询这批单号中维修了多少个
     * @return
     */
    public List<Maintenance>  getMaintenanceByRwdbhBath(List<String> bsbhList){
        // 构建查询条件
        Query query = new Query();
        query.addCriteria(Criteria.where("bsbh").in(bsbhList));
        query.addCriteria(new Criteria("hyzt").is(1));
        List<Maintenance> list = mongoTemplate.find(query, Maintenance.class, Maintenance.TableName); // 取得满足条件的数据
        return list;

    }


    /**
     * 删除维修记录
     * @return
     */
    public long deleteMainte(String _id){
        Query query = Query.query(Criteria.where("_id").is(_id));
        DeleteResult deleteResult = mongoTemplate.remove(query, Maintenance.class);
        long res = deleteResult.getDeletedCount();
        return res;
    }

}
